package com.insigma.business.qrcode.intell._provider.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.intell._provider.CommonPolicyProvider;
import com.insigma.business.qrcode.intell.controller.BaseProviderImpl;
import com.insigma.business.qrcode.intell.dto.*;
import com.insigma.business.qrcode.intell.entity.CommonPolicyDO;
import com.insigma.business.qrcode.intell.entity.CommonQaInfoDO;
import com.insigma.business.qrcode.intell.service.CommonPolicyService;
import com.insigma.business.qrcode.common.converter.CommonPolicyProviderConverter;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class CommonPolicyProviderImpl extends BaseProviderImpl implements CommonPolicyProvider {

    @Resource
    CommonPolicyService commonPolicyService;

    @Override
    public BasePagingResponse<List<CommonPolicyDTO>> searchCommonPolicy(CommonPolicySearchRequest commonPolicySearchRequest) {
        final BasePagingResponse<List<CommonPolicyDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(commonPolicySearchRequest, "查询分页CommonPolicy的请求参数不能为空");
            }

            @Override
            public void executeService() {
                final CommonPolicyBSearcher commonPolicyBSearcher = CommonPolicyProviderConverter.convertRequestSO2Searcher(commonPolicySearchRequest);
                PageHelper.startPage(commonPolicySearchRequest.getPageNo(), commonPolicySearchRequest.getPageSize());
                final List<CommonPolicyDO> commonPolicyDOList = commonPolicyService.search(commonPolicyBSearcher);
                PageInfo<CommonPolicyDO> pageinfo = new PageInfo<>(commonPolicyDOList);
                final List<CommonPolicyDTO> commonPolicyDTOList = CommonPolicyProviderConverter.convertResponseDOList2DTOList(commonPolicyDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, commonPolicyDTOList, commonPolicySearchRequest,pageinfo.getTotal());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_SEARCH;
            }
        });

        return basePagingResponse;    }



    @Override
    public BaseResponse<CommonPolicyDTO> getCommonPolicy(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonPolicyDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final CommonPolicyDO commonPolicyDO = commonPolicyService.get((String)baseGetRequest.getId());
                final CommonPolicyDTO remoteObj = CommonPolicyProviderConverter.convertResponseDO2DTO(commonPolicyDO);
                ResponseUtils.setupSuccessResponse(baseResponse,remoteObj);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonPolicy(final CommonPolicyInsertRequest insertRequest){

        if(insertRequest.getPublished() == null){
            insertRequest.setPublished(false);
        }
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonPolicy的请求参数不能为空");
            }
            @Override
            public void executeService() {
                final CommonPolicyDO commonPolicyDO = CommonPolicyProviderConverter.convertRequestIO2DO(insertRequest);
                if(Objects.isNull(commonPolicyDO.getTop())){
                    commonPolicyDO.setTop(Boolean.FALSE);
                }
                if(commonPolicyDO.getTop()){
                    commonPolicyDO.setTopWeight(getMaxTopWeight());
                }else {
                    commonPolicyDO.setTopWeight(0);
                }

                if (Objects.isNull(commonPolicyDO.getPublished())) {
                    commonPolicyDO.setPublished(Boolean.FALSE);
                }

                commonPolicyService.insert(commonPolicyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, commonPolicyDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");
            }
            @Override
            public void executeService() {
                CommonPolicyDO commonPolicyCurrentDO = commonPolicyService.get(updateRequest.getId());
                BizCheckUtil.checkNotNull(commonPolicyCurrentDO, "数据不存在");
                CommonPolicyDO commonPolicyDO = CommonPolicyProviderConverter.convertRequestUO2DO(updateRequest);
                if(Objects.isNull(commonPolicyDO.getTop())){
                    commonPolicyDO.setTop(Boolean.FALSE);
                }
                if (Boolean.TRUE.equals(commonPolicyDO.getTop())) {
                    commonPolicyDO.setTopWeight(commonPolicyCurrentDO.getTopWeight());
                } else {
                    commonPolicyDO.setTopWeight(0);
                }

                Integer affectRowNum = commonPolicyService.update(commonPolicyDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_UPDATE;
            }
        });
        return baseResponse;
    }



    @Override
    public BaseResponse<Integer> publishCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        BizCheckUtil.checkNotNull(updateRequest, "修改CommonPolicy的请求参数不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");

        CommonPolicyDO commonPolicyDO = commonPolicyService.get(updateRequest.getId());
        BizCheckUtil.checkNotNull(commonPolicyDO, "数据不存在");

        commonPolicyDO.setPublished(updateRequest.getPublished());
        commonPolicyDO.setModifierId(updateRequest.getModifierId());
        commonPolicyDO.setModifierType(updateRequest.getModifierType());
        commonPolicyDO.setModifyDatetime(updateRequest.getModifyDatetime());
        Integer update = commonPolicyService.update(commonPolicyDO);

        return ResponseUtils.buildSuccessResponse(update);
    }


    @Override
    public BaseResponse<Integer> topCommonPolicy(final CommonPolicyUpdateRequest updateRequest){

        BizCheckUtil.checkNotNull(updateRequest, "修改CommonPolicy的请求参数不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierId(),"操作人不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierType(),"操作人类型不能为空");


        CommonPolicyDO commonPolicyDO = commonPolicyService.get(updateRequest.getId());
        BizCheckUtil.checkNotNull(commonPolicyDO, "数据不存在");

        if(updateRequest.getTop()){
            commonPolicyDO.setTopWeight(getMaxTopWeight());
        }else {
            commonPolicyDO.setTopWeight(0);
        }

        commonPolicyDO.setTop(updateRequest.getTop());
        commonPolicyDO.setModifierId(updateRequest.getModifierId());
        commonPolicyDO.setModifierType(updateRequest.getModifierType());
        commonPolicyDO.setModifyDatetime(updateRequest.getModifyDatetime());
        Integer update = commonPolicyService.update(commonPolicyDO);

        return ResponseUtils.buildSuccessResponse(update);
    }

    private Integer getMaxTopWeight() {
        Integer maxTopWeight = commonPolicyService.getMaxTopWeight();
        if(maxTopWeight == null){
            maxTopWeight = 1;
        }else {
            maxTopWeight += 1;
        }
        return maxTopWeight;
    }



    @Override
    public BaseResponse<Integer> deleteCommonPolicy(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonPolicy的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(),"操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getModifierId(),"操作人不能为空");
            }
            @Override
            public void executeService() {
                final Integer affectRowNum = commonPolicyService.delete((String)deleteRequest.getId(),deleteRequest.getModifierType(),(String)deleteRequest.getModifierId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_POLICY_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }


}
